<?php
require_once("../../Config.php");
require_once("../../Class/ClassFunc.php");
require_once("../../Class/ClassMysql.php");
require_once("../../Class/ClassCheck.php");
require_once("../../Class/ClassResult.php");

//json头
header("Content-type: application/json");

session_start();
if(!isset($_SESSION["name"])) {
    exit(Result::jsonReturn(1, "未登录"));
}

$uid = $_SESSION["id"];
$uname = $_SESSION["name"];
$uemail = $_SESSION["email"];
$upower = $_SESSION["power"];
$info = array();

if(isset($_POST["email"]) && !Check::isEmpty($_POST["email"])) {
    if(Check::isEmail($_POST["email"])) {
        $info["email"] = $_POST["email"];
    }else {
        exit(Result::jsonReturn(1, "邮箱格式有误"));
    }    
}
if(isset($_POST["password"]) && !Check::isEmpty($_POST["password"])) {
    if(Check::isPassword($_POST["password"])) {
        $info["password"] = Func::encipher($_POST["password"]);
    }else {
        exit(Result::jsonReturn(1, "密码格式错误"));
    }
}
if(count($info) == 0) {
    exit(Result::jsonReturn(1, "无更新内容"));
}
$sql = "UPDATE qilian_users SET ";
foreach($info as $key=>$value) {
    switch($key) {
        case "email":
            $sql = $sql."{$key}='{$value}',";
            break;
        case "password":
            $sql = $sql."{$key}='{$value}',";
            break;
        default: 
            break;
    }
}
$sql = Func::removeComma($sql);
$sql = $sql." WHERE id={$uid}";
$db = new DB();
if($db->update($sql)) {
    session_start();
    session_destroy();
    exit(Result::jsonReturn(0, "更新成功，请重新登录", array(
        "id"=>$uid)));
}else {
    exit(Result::jsonReturn(1, "更新失败"));
}


?>